﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace Tomahawk.Runtime.Logic
{
    public sealed class XAudioListenerComponent : XComponent
    {
        #region Properties
        private AudioListener audioListener = new AudioListener();
        public AudioListener AudioListener { get { return audioListener; } }

        public bool Enabled
        {
            get
            {
                return (Engine.Instance.AudioManager.AudioListener == audioListener);
            }

            set
            {
                if (!Enabled && value)
                {
                    Engine.Instance.AudioManager.AudioListener = audioListener;
                }
            }
        }
        #endregion

        #region Initialization
        public XAudioListenerComponent()
        {
            UpdateRequired = true;
        }
        #endregion

        #region XObject
        protected override void OnUpdate(float elapsedTime)
        {
            base.OnUpdate(elapsedTime);

            Vector3 deltaMove = (Owner.SceneComponent.SceneNode.Position - audioListener.Position);

            audioListener.Velocity = elapsedTime > 0.0f ? deltaMove / elapsedTime : Vector3.Zero;
            audioListener.Position = Owner.SceneComponent.SceneNode.Position;
            audioListener.Forward = Owner.SceneComponent.SceneNode.WorldMatrix.Forward;
            audioListener.Up = Owner.SceneComponent.SceneNode.WorldMatrix.Up;
        }
        #endregion
    }
}
